
var AccountInfoDialog = {

    setupFormValidation: function () {

        this.studentFormValidator = $("#form_student_info").validate({
            rules: {
                'studentNumber:string': {
                    required: true,
                    maxlength: 10,
                    pattern: '[A-Za-z0-9]+'
                },
                'name:string': {
                    required: true,
                    maxlength: 50
                },
                'idNumber:string': {
                    required: true,
                    pattern: '\\d{17}[\\d|X]|\\d{15}'
                },
                'birthDate:string': {
                    required: false,
                    pattern: '\\d{4}-\\d{2}-\\d{2}'
                },
                'phone:string': {
                    required: false,
                    pattern: '(13|14|15|17|18|19)[0-9]{9}'
                }
            },
            messages: {
                'studentNumber:string': {
                    required: '必填',
                    maxlength: '学号最多 10 个字符',
                    pattern: '[A-Za-z0-9]+'
                },
                'name:string': {
                    required: '必填',
                    maxlength: '名字最多 50 个字符'
                },
                'idNumber:string': {
                    required: '必填',
                    pattern: '请输入正确的身份证，最后一位为数字或大写字母 X'
                },
                'birthDate:string': {
                    pattern: '请输入正确的日期，例如: 1990-01-01'
                },
                'phone:string': {
                    pattern: '请输入正确的国内手机号'
                }
            }
        });
        this.teacherFormValidator = $("#form_teacher_info").validate({
            rules: {
                'teacherNumber:string': {
                    required: true,
                    maxlength: 10,
                    pattern: '[A-Za-z0-9]+'
                },
                'name:string': {
                    required: true,
                    maxlength: 50
                },
                'idNumber:string': {
                    required: true,
                    pattern: '\\d{17}[\\d|X]|\\d{15}'
                },
                'birthDate:string': {
                    required: false,
                    pattern: '\\d{4}-\\d{2}-\\d{2}'
                },
                'phone:string': {
                    required: false,
                    pattern: '(13|14|15|17|18|19)[0-9]{9}'
                }
            },
            messages: {
                'teacherNumber:string': {
                    required: '必填',
                    maxlength: '教师工号最多 10 个字符',
                    pattern: '[A-Za-z0-9]+'
                },
                'name:string': {
                    required: '必填',
                    maxlength: '名字最多 50 个字符'
                },
                'idNumber:string': {
                    required: '必填',
                    pattern: '请输入正确的身份证，最后一位为数字或大写字母 X'
                },
                'birthDate:string': {
                    pattern: '请输入正确的日期，例如: 1990-01-01'
                },
                'phone:string': {
                    pattern: '请输入正确的国内手机号'
                }
            }
        });
    },
    resetAccountInfoDialog: function () {
        $('#dialog form').each(function () {
            this.reset();
        });

        this.teacherFormValidator.resetForm();
        this.studentFormValidator.resetForm();
        $('input.uk-form-danger').removeClass('uk-form-danger');
    },
    fillUpForms: function (account) {
        if (account.userType === 'S' && account.student != null) {
            $('#account_user_type')[0].value = 'S';
            $('#student_no')[0].value = account.student.studentNumber;
            $('#student_name')[0].value = account.student.name;
            $('#student_birth_date')[0].value = objectToString(account.student.birthDate);
            $('#student_gender')[0].value = account.student.gender;
            $('#student_id_number')[0].value = account.student.idNumber;
            $('#student_phone')[0].value = objectToString(account.student.phone);
            $('#student_college')[0].value = objectToString(account.student.collegeId, 0);
            $('#student_department')[0].value = objectToString(account.student.departmentId, 0);
            $('#student_enrollment_year')[0].value = account.student.enrollmentYear;
            this.changeUserType('S');
        } else if (account.userType === 'T' && account.teacher != null) {
            $('#account_user_type')[0].value = 'T';
            $('#teacher_no')[0].value = account.teacher.teacherNumber;
            $('#teacher_name')[0].value = account.teacher.name;
            $('#teacher_birth_date')[0].value = objectToString(account.teacher.birthDate);
            $('#teacher_gender')[0].value = account.teacher.gender;
            $('#teacher_id_number')[0].value = account.teacher.idNumber;
            $('#teacher_phone')[0].value = objectToString(account.teacher.phone);
            $('#teacher_college')[0].value = objectToString(account.teacher.collegeId, 0);
            $('#teacher_department')[0].value = objectToString(account.teacher.departmentId, 0);
            $('#teacher_title')[0].value = account.teacher.academicTitle;
            $('#teacher_degree')[0].value = account.teacher.highestDegree;
            this.changeUserType('T')
        }
    },
    changeUserType: function (userType) {
        if (userType === undefined) {
            $('#form_student_info').hide();
            $('#form_teacher_info').hide();
        } else if (userType === "S") {
            $('#form_student_info').show();
            $('#form_teacher_info').hide();
        } else if (userType === "T")  {
            $('#form_student_info').hide();
            $('#form_teacher_info').show();
        }
    },
    addSaveButtonClickHandler: function (handler) {
        $('#btn_save_account').click(handler);
    },
    showDialog: function () {
        UIkit.modal('#dialog').show();
    },
    hideDialog: function () {
        UIkit.modal('#dialog').hide();
    }
};

var accountList = {
    accounts: [],

    add: function (account) {
        if (account != null) {
            this.accounts.push(account);
            var tableRow = accountToTableRow(account, accountList.accounts.length);
            $('#tbody_account_list').append(tableRow);
        }
    },

    /**
     * 删除一个 account，包括 accounts 数组的对象以及表格中的行
     * @param index 要删除的 account 在 accounts 中的索引，从 0 开始
     */
    remove: function (index) {
        index = parseInt(index);
        if (index >= 0 && index < this.accounts.length) {
            this.accounts.splice(index, 1);
            $("#tbody_account_list tr:nth-child(" + (index + 1) + ")").remove();
            this.updateTableIndex(index);
        }
    },

    /**
     * 替换一个 account，包括 accounts 数组的对象以及表格中的行
     * @param account 替换后的 account 对象
     * @param index 被替换的 account 在 accounts 中的索引
     */
    replace: function (account, index) {
        index = parseInt(index);
        if (account != null && index >= 0 && index < this.accounts.length) {
            this.accounts.splice(index, 1, account);
            var tableRow = accountToTableRow(account, index + 1);
            $('#tbody_account_list tr:nth-child(' + ++index + ')').replaceWith(tableRow);
        }
    },

    clear: function () {
        $('#tbody_account_list tr').remove();
        accountList.accounts = [];
    },

    /**
     * 从指定位置开始更新新用户表格中的序号
     * @param from 开始更新表格行的行号 (包含)，从 0 开始
     */
    updateTableIndex: function(from) {
        if (from < 0)
            from = 0;
        var rows = $("#tbody_account_list td:first-of-type");
        for (var i = from; i < rows.length; i++) {
            $(rows[i]).text(i + 1);
        }
    }
};

function objectToString(o, defaultVal) {
    if (defaultVal == null)
        defaultVal = "";
    return o == null ? defaultVal : o.toString();
}